var devTypeMapList = {0 : "互动屏", 1 : "非互动屏"};


//获得站点信息
function getDevInfo(type){
    var tbody=window.document.getElementById("dev_data");
    $.ajax({
        url:"../../dev/site/"+type,
        type:"GET",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="";
                var tdata=data.object;
                for( i in tdata){
                    var j = parseInt(i)+1;
                    str +="<tr>"+"" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ j +"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+tdata[i].ownId+"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+tdata[i].name+"</td>" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+tdata[i].height+"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+tdata[i].width+"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ tdata[i].position.province + "-"+ tdata[i].position.city + "-"+ tdata[i].position.district + "</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ "经度: " + tdata[i].longitude + " 纬度: " + tdata[i].latitude + "</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+devTypeMapList[tdata[i].type]+"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">" +
                        "<input type='button' value='编辑' id='edit_button'+" + tdata[i].id + " onclick='editButtonClick(" + tdata[i].id + ")'" +"</input>"+
                        "<input type='button' value='删除' id='del_button'+" + tdata[i].id + " onclick='deleteButtonClick( " + tdata[i].id + ")'" +"</input>";
                    if (type == 1) {
                        str += "<input type='button' value='移除资源位' id='rm_res_button'+" + tdata[i].id + " onclick='rmResButtonClick( " + tdata[i].id + "," + type + ")'" +"</input></td>";
                    }
                    if (type == 2) {
                        str += "<input type='button' value='设置资源位' id='rm_res_button'+" + tdata[i].id + " onclick='setResButtonClick( " + tdata[i].id + ")'" +"</input></td>";
                    }
                    str += "</tr>";
                }
                tbody.innerHTML =str;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });
}

function addButtonClick() {
    location.href="./dev_add.html";
}

function addSubmitButtonClick() {
    var fdata = $("#dev_add_form").serialize();
    $.param(fdata);
    //console.log(fdata);
    $.ajax({
        url:"../../dev/new",
        type:"POST",
        data:fdata,
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(result){
            if(result.code==0){
                alert(result.message);
                location.href="./dev_list.html";
            }else if(result.code==1){
                alert(result.message);
            }
        }
    });
}

//      删除已选设备
function deleteButtonClick(id){
    var fdata = $("#devs_form").serialize();
    $.param(fdata);
    $.ajax({
        url:"../../dev/deletion/" + id,
        type:"GET",
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(result){
            if(result.code==0){
                alert(result.message);
                location.href="./dev_list.html";
            }else if(result.code==1){
                alert(result.message);
            }
        }
    });

}

// 编辑时，获取id号对应的设备信息，并设初值
function setOriginInfo() {
    var url = window.location.href;
    var site_id = getArgsFromHref(url, "id");
    $.ajax({
        url:"../../template/" + site_id,
        type:"GET",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var tdata=data.object;
                document.getElementById("_id").value=tdata.id;
                document.getElementById("_ownId").value=tdata.ownId;
                document.getElementById("_name").value=tdata.name;
                document.getElementById("_height").value=tdata.height;
                document.getElementById("_width").value=tdata.width;
                document.getElementById("_country").value=tdata.position.country;
                document.getElementById("_province").value=tdata.position.province;
                document.getElementById("_city").value=tdata.position.city;
                document.getElementById("_district").value=tdata.position.district;
                document.getElementById("_longitude").value=tdata.longitude;
                document.getElementById("_latitude").value=tdata.latitude;

                if (tdata.type == 0) {
                    document.getElementById("_type0").click();
                } else {
                    document.getElementById("_type1").click();
                }
            }
        },
        error:function(){
            alert("search fails！");
        }
    });

}

// 点击编辑按钮时，跳转到编辑页面
function editButtonClick(id){
    location.href="./dev_edit.html?id=" + id;
}

// 编辑时，点击提交按钮
function editSubmitButtonClick(){
    var fdata = $("#template_edit_form").serialize();
    $.param(fdata);
    $.ajax({
        url:"../../template/edit",
        type:"POST",
        data:fdata,
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(result){
            if(result.code==0){
                alert(result.message);
                location.href="./template_list.html";
            }else if(result.code==1){
                alert(result.message);
                return;
            }
        }
    });
}

// 获取url中的某个参数
function getArgsFromHref(sHref, sArgName)
{
    var args = sHref.split("?");
    var retval = "";
    if(args[0] == sHref) /*参数为空*/
    {
        return retval; /*无需做任何处理*/
    }
    var str = args[1];
    args = str.split("&");
    for(var i = 0; i < args.length; i ++)
    {
        str = args[i];
        var arg = str.split("=");
        if(arg.length <= 1) continue;
        if(arg[0] == sArgName) retval = arg[1];
    }
    return retval;
}

function rmResButtonClick(devId, type) {
    var fdata = $("#devs_form").serialize();
    $.param(fdata);
    $.ajax({
        url:"../../loc/deletion/dev/" + devId,
        type:"GET",
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(result){
            if(result.code==0){
                alert(result.message);
                getDevInfo(type);
            }else if(result.code==1){
                alert(result.message);
            }
        }
    });
}

